﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DapperExtensions;
using DapperWrapper;
using ZLWeb.WebApiEntity;
using ZLWeb.WebApiEntity.EasyUse;
using ZLWeb.WebApiRepository.InterFace;

namespace ZLWeb.WebApiRepository
{
    public class ChargeRepository : Respository<Zzinfocs>, IChargeRepository
    {
        public IEnumerable<Zzinfocs> GetChargeInfo(string cgsid, short useplace)
        {
            Zzinfocs zzinfocs = new Zzinfocs();
            IEnumerable<Zzinfocs> items = ExecuteProcWithTemptable(zzinfocs, "pr_Zengzhl_Query", "#zzinfo", false,
                new {cgsid, useplace});
            return items;
        }

        public AccCzResponse PreCharge(ThrywInfo ywInfo, string cardno, short ctype)
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            var ac = ExecuteProcAddParam(ywInfo, "#ywInfo", true);
            paActions.Add(ac);
            dpno pno=new dpno();
            var ywInfoparam = ExecuteProcAddParam(pno, "#pno", false);
            paActions.Add(ywInfoparam);
            AccCzResponse cx=new AccCzResponse();
            ExecuteProc(paActions, "pr_AccThrCz_PreCz", new { cardno, ctype }, (con) =>
            {
                cx.ywInfo = con.GetList<ThrywInfo>().FirstOrDefault();
                cx.pno = con.GetList<dpno>().FirstOrDefault();
            });
            return cx;
        }

        public Thrpaylog ChargeNewSave(Thrpaylog log,short payway)
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            if (log.alog != null)
            {
                var ac = ExecuteProcAddParam(log.alog, "#alilog", true);
                paActions.Add(ac);
            }
            else
            {
                Alilog sb=new Alilog();
                var ac = ExecuteProcAddParam(sb, "#alilog", false);
                paActions.Add(ac);
            }
            if (log.xlog != null)
            {
                var ywInfoparam = ExecuteProcAddParam(log.xlog, "#wxlog", true);
                paActions.Add(ywInfoparam);
            }
            else
            {
                Wxlog sb = new Wxlog();
                var ac = ExecuteProcAddParam(sb, "#wxlog", false);
                paActions.Add(ac);
            }
            Thrpaylog tl=new Thrpaylog();
            ExecuteProc(paActions, "pr_Thrpaylog_newsave", new { payway } , (con) =>
            {
                tl.alog = con.GetList<Alilog>().FirstOrDefault();
                tl.xlog = con.GetList<Wxlog>().FirstOrDefault();
            });
            return tl;
        }

        public Thrpaylog ChargeModifySave(Thrpaylog log, short payway,string paysno)
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            if (log.alog != null)
            {
                var ac = ExecuteProcAddParam(log.alog, "#alilog", true);
                paActions.Add(ac);
            }
            else
            {
                Alilog sb = new Alilog();
                var ac = ExecuteProcAddParam(sb, "#alilog", false);
                paActions.Add(ac);
            }
            if (log.xlog != null)
            {
                var ywInfoparam = ExecuteProcAddParam(log.xlog, "#wxlog", true);
                paActions.Add(ywInfoparam);
            }
            else
            {
                Wxlog sb = new Wxlog();
                var ac = ExecuteProcAddParam(sb, "#wxlog", false);
                paActions.Add(ac);
            }
            Thrpaylog tl = new Thrpaylog();
            ExecuteProc(paActions, "pr_Thrpaylog_modsave", new { payway, paysno }, null);
            return tl;
        }

        public Result ChargeResultQuery(short payway, string pno,string pnopwd)
        {
            Result resitem = new Result();
            Result ritem = ExecuteProcWithTemptable(resitem, "pr_ThrPayResult_query", "#result", false,
                new {payway, pno, pnopwd}).FirstOrDefault();
            return ritem;
        }

        public void ChargeCancel( string pno, string pnopwd)
        {
            ExecuteProc("pr_AccCancel_ByPno", new { pno, pnopwd });
        }
    }
}
